import os
import re


class NewRegedit(object):

    def get_file_list(self, base_path):
        file_dir = []
        file_list = []
        file_deal_list = []

        for root, dir, files in os.walk(base_path):
            for item in dir:
                data = {"root": item, "dirs": ['20220101', '20220102', '20220103', '20220104', '20220105', '20220106', '20220107', '20220108', '20220109', '20220110', '20220111', '20220112', '20220113', '20220114', '20220115', '20220116', '20220117', '20220118', '20220119', '20220120', '20220121', '20220122', '20220123', '20220124', '20220125', '20220126', '20220127', '20220128', '20220129', '20220130', '20220131', '20220201', '20220202', '20220203', '20220204', '20220205', '20220206', '20220207', '20220208', '20220209', '20220210', '20220211', '20220212', '20220213', '20220214', '20220215', '20220216', '20220217', '20220218', '20220219', '20220220', '20220221', '20220222', '20220223', '20220224', '20220225', '20220226', '20220227', '20220228', '20220301', '20220302', '20220303', '20220304', '20220305', '20220306', '20220307', '20220308', '20220309', '20220310', '20220311', '20220312', '20220313', '20220314', '20220315', '20220316', '20220317', '20220318', '20220319', '20220320', '20220321', '20220322', '20220323', '20220324', '20220325', '20220326', '20220327', '20220328', '20220329', '20220330', '20220331']}
                file_dir.append(data)
            break

        print(file_dir)

        for item in file_dir:
            r = item["root"]
            for dir1 in item["dirs"]:
                path = rf'{base_path}\{r}\{dir1}'
                print(path)
                for root, dir, files in os.walk(path):
                    for item in files:
                        data = {"filename": item, "fileabs": rf'{root}\{item}'}
                        file_list.append(data)

        for item in file_list:
            path_abs = item["fileabs"]
            with open(path_abs, 'r', encoding='utf-8') as f:
                file_lines = f.read()
                try:
                    is_member = re.findall('<bIsNewCustomer>(.*)</bIsNewCustomer>', file_lines)
                    if len(is_member) == 0:
                        continue
                    if is_member[0] == "-1":
                        lRetailStoreID = re.findall('<lRetailStoreID>(.*)</lRetailStoreID>', file_lines)[0]
                        lTaNmbr = re.findall('<lTaNmbr>(.*)</lTaNmbr>', file_lines)[0]
                        lWorkstationNmbr = re.findall('<lWorkstationNmbr>(.*)</lWorkstationNmbr>', file_lines)[0]
                        lTaCreatedOperatorID = re.findall('<lTaCreatedOperatorID>(.*)</lTaCreatedOperatorID>', file_lines)[0]
                        szMobilePhone = re.findall('<szMobilePhone>(.*)</szMobilePhone>', file_lines)[0]
                        szCardID = re.findall('<szCardID>(.*)</szCardID>', file_lines)[0]
                        szCustomerId = re.findall('<szCustomerId>(.*)</szCustomerId>', file_lines)[0]
                        szTaCreatedDate = re.findall('<szTaCreatedDate>(.*)</szTaCreatedDate>', file_lines)[0]
                        szDate = szTaCreatedDate[0:8]
                        szTime = szTaCreatedDate[8:]
                        dict1 = {
                            "lRetailStoreID":lRetailStoreID,
                            "lTaNmbr": lTaNmbr,
                            "lWorkstationNmbr": lWorkstationNmbr,
                            "lTaCreatedOperatorID": lTaCreatedOperatorID,
                            "szMobilePhone": szMobilePhone,
                            "szCardID": szCardID,
                            "szCustomerId": szCustomerId,
                            "szDate": szDate,
                            "szTime": szTime
                        }
                        f.close()
                        with open("data.csv", 'a') as f1:
                            f.write(rf"{lRetailStoreID},{lTaNmbr},{lWorkstationNmbr},{lTaCreatedOperatorID},{szMobilePhone},{szCardID},{szCustomerId},{szDate},{szTime}" + "\n")
                        f1.close()
                        file_deal_list.append(dict1)
                except Exception as e:
                    print(e)

if __name__ == '__main__':
    base_path = rf'D:\Disktop\新建文件夹 (10)'
    # base_path = input(r'')
    n = NewRegedit()
    n.get_file_list(base_path)